package com.zbw.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.zbw.common.entity.UserFeedback;
import com.zbw.common.mapper.FrontUserBookshelfMapper;
import com.zbw.common.mapper.FrontUserReadHistoryMapper;
import com.zbw.common.mapper.UserFeedbackDynamicSqlSupport;
import com.zbw.common.mapper.UserFeedbackMapper;
import com.zbw.common.service.UserPageService;
import com.zbw.common.vo.BookReadHistoryVO;
import com.zbw.common.vo.BookShelfVO;
import com.zbw.common.vo.UserFeedbackVO;
import io.github.xxyopen.model.page.PageBean;
import io.github.xxyopen.model.page.builder.pagehelper.PageBuilder;
import io.github.xxyopen.web.util.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.zbw.common.mapper.UserFeedbackDynamicSqlSupport.userFeedback;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.select.SelectDSL.select;

/**
 * @Description
 * @Author Zbw
 * @Date 2024/6/20
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserPageImpl implements UserPageService {

   private final FrontUserBookshelfMapper userBookshelfMapper;

   private final FrontUserReadHistoryMapper userReadHistoryMapper;

   private final UserFeedbackMapper userFeedbackMapper;
    @Override
    public PageBean<BookShelfVO> listBookShelfByPage(Long userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        return PageBuilder.build(userBookshelfMapper.listBookShelf(userId));
    }

    @Override
    public PageBean<BookReadHistoryVO> listReadHistoryByPage(Long id, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        return PageBuilder.build(userReadHistoryMapper.listReadHistory(id));
    }

    @Override
    public PageBean<UserFeedback> listUserFeedBackByPage(Long userId, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        SelectStatementProvider selectStatement = select(UserFeedbackDynamicSqlSupport.content,
                UserFeedbackDynamicSqlSupport.createTime)
                .from(userFeedback)
                .where(UserFeedbackDynamicSqlSupport.userId, isEqualTo(userId))
                .orderBy(UserFeedbackDynamicSqlSupport.id.descending())
                .build()
                .render(RenderingStrategies.MYBATIS3);
        List<UserFeedback> userFeedbacks = userFeedbackMapper.selectMany(selectStatement);
        PageBean<UserFeedback> pageBean = PageBuilder.build(userFeedbacks);
        pageBean.setList(BeanUtil.copyList(userFeedbacks, UserFeedbackVO.class));
        return pageBean;
    }
}
